# Copyright 2010-2024 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# We use a lot of macros to reduce the duplication between scip and scip_parallel.

PLATFORM_FLAGS = select({
    "@platforms//os:linux": [
        "-Wunknown-pragmas",
        "-fexceptions",
        "-DSYM=bliss",
        "-DBLISS_VERSION=\\\"0.73\\\"",
    ],
    "@platforms//os:macos": [
        "-Wunknown-pragmas",
        "-fexceptions",
        "-DSYM=bliss",
        "-DBLISS_VERSION=\\\"0.73\\\"",
    ],
    "@platforms//os:windows": [
        "/DSYM=none",
        "/DSCIP_NO_SIGACTION",
        "/DSCIP_NO_STRTOK_R",
        "/utf-8"
    ],
    "//conditions:default": [],
})

PLATFORM_DEPS = select({
    "@platforms//os:linux": ["@bliss"],
    "@platforms//os:macos": ["@bliss"],
    "@platforms//os:windows": [],
    "//conditions:default": [],
})

BLISS_FILE = select({
    "@platforms//os:linux": ["src/symmetry/compute_symmetry_bliss.cpp"],
    "@platforms//os:macos": ["src/symmetry/compute_symmetry_bliss.cpp"],
    "@platforms//os:windows": ["src/symmetry/compute_symmetry_none.cpp"],
    "//conditions:default": ["src/symmetry/compute_symmetry_none.cpp"],
})

BASE_SRCS = glob(
    [
        "src/*/*.c",
    ],
    exclude = [
        "src/lpi/lpi_*.c",
        "src/nauty/*",
        "src/scip/exprinterpret_*.c",
        "src/scip/nlpi_filtersqp.c",
        "src/scip/nlpi_worhp.c",
        "src/scip/*_xyz.c",
        "src/scip/sorttpl.c",
        "src/symmetry/compute_symmetry_*.cpp",
        "src/symmetry/*nauty*",
        "src/tpi/tpi_*.c",
    ],
)

BASE_HDRS = glob(
    [
        "src/*/*.h",
        "src/*/*.hpp",
    ],
    exclude = [ "src/scip/*_xyz.h" ],
) + [
        "src/scip/githash.c",
        "src/scip/sorttpl.c",
]

BASE_COPTS = [
    "-DSCIP_IPOPT=\\\"NONE\\\"",
    "-DSCIP_LPS=\\\"spx2\\\"",
    "-DSCIP_GITHASH=\\\"a740f0891e\\\"",
]


DEFINES = [
    # Scip optionally depends on scip/config.h and scip/scip_export.h
    # that are generated by build system.
    #
    # We need every library and binary that depends on SCIP libraries to
    # define this macro. That is why we use `defines' here instead of
    # `copts' or `local_defines'.
    "NO_CONFIG_HEADER",
]

cc_library(
    name = "scip",
    srcs =  BASE_SRCS + BLISS_FILE + [
        "src/lpi/lpi_spx2.cpp",
        "src/scip/exprinterpret_none.c",
        "src/tpi/tpi_none.c",
    ],
    hdrs = BASE_HDRS,
    copts = BASE_COPTS + [
        "-DTPI_NONE",  # src/tpi/tpi_none.h
        "-DNPARASCIP",
    ] + PLATFORM_FLAGS,
    defines = DEFINES,
    includes = ["src"],
    visibility = ["//visibility:public"],
    deps = [
        "@soplex",
        "@zlib",
    ] + PLATFORM_DEPS,
)

cc_library(
    name = "scip_parallel",
    srcs = BASE_SRCS + BLISS_FILE + [
        "src/scip/exprinterpret_none.c",
        "src/tpi/tpi_tnycthrd.c",
    ],
    hdrs = BASE_HDRS,
    copts = BASE_COPTS + [
        "-DTPI_TNY",  # src/tpi/tpi_tnycthrd.h
        "-DPARASCIP",
    ] + PLATFORM_FLAGS,
    defines = DEFINES,
    includes = ["src"],
    visibility = ["//visibility:public"],
    deps = [
        "@soplex",
        "@zlib",
    ] + PLATFORM_DEPS,
)

cc_binary(
    name = "scip_interactive",
    srcs = ["src/cppmain.cpp"],
    deps = [":scip"],
)

